package com.example.oasystem.controller.login;


import com.example.oasystem.dao.login.AoaUserLoginRecordDao;
import com.example.oasystem.dao.notepaper.AoaNotepaperDao;
import com.example.oasystem.entity.notepaper.AoaNotepaper;
import com.example.oasystem.entity.user.AoaUser;
import com.example.oasystem.entity.user.AoaUserLoginRecord;
import com.example.oasystem.service.AoaUserService;
import nl.bitwalker.useragentutils.Browser;
import nl.bitwalker.useragentutils.OperatingSystem;
import nl.bitwalker.useragentutils.UserAgent;
import nl.bitwalker.useragentutils.Version;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@SuppressWarnings("all")
public class LoginController {
    @Autowired
    private AoaUserService aoaUserService;
    @Autowired
    AoaNotepaperDao aoaNotepaperDao;
    @Autowired
    AoaUserLoginRecordDao aoaUserLoginRecordDao;

    @RequestMapping("/")
    public String show(HttpServletRequest request){

        return "login/login";
    }

    @RequestMapping("/logins")
    public String logins(@RequestParam("userName") String userName, @RequestParam("password") String password,@RequestParam("code") String code, Model model, HttpServletRequest request) throws Exception{

        AoaUser aoaUser=aoaUserService.getAoaUser(userName,password);
        System.out.println(aoaUser);
        if (StringUtils.isEmpty(aoaUser))
        {
            System.out.println("用户名或密码错误");
            model.addAttribute("errormess","账号或密码错误");
            return "login/login";
        }
        else
        {
//            if (code.equals(request.getSession().getAttribute("code")))
//            {
            //ip

            String ip=InetAddress.getLocalHost().getHostAddress();
            //浏览器信息
            Browser browser = UserAgent.parseUserAgentString(request.getHeader("User-Agent")).getBrowser();
            Version version = browser.getVersion(request.getHeader("User-Agent"));
            String info = browser.getName() + "/" + version.getVersion();
            //ip 浏览器 time userid
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String loginTime=df.format(new Date());
            aoaUserLoginRecordDao.insertSelective(new AoaUserLoginRecord(null,info,ip,loginTime,null,aoaUser.getUserId(),null));
            request.getSession().setAttribute("user",aoaUser);
            System.out.println("aoaUser = " + aoaUser);
            return "forward:/index";
//           }
//            else
//            {
//                System.out.println("验证码错误");
//                model.addAttribute("errormess","验证码错误");
//                return "login/login";
//            }

        }
    }

}
